from clusterer.clusterer import Clusterer
from sklearn.cluster import KMeans


# K均值
class Kmeans(Clusterer):
    # k值
    k_value = 5
    # 簇中心点
    cluster_centers = []
    
    def __init__(self):
        Clusterer.__init__(self)
        self.algorithm_name = "K均值"
        self.ipynb_template_name = "kmeans-template.ipynb"
        
    def implent(self): 
        Clusterer.implent(self)
        # 构造模型
        self.algorithm = KMeans(n_clusters=self.k_value)
        # 分类
        self.algorithm.fit(self.inputs)
        # 结果
        self.label_list = self.algorithm.labels_
        self.cluster_count = len(set(self.algorithm.labels_))
        self.cluster_centers = self.algorithm.cluster_centers_
        # 保存xlsx
        self.saveToExcle()
        # 绘图
        self.drawChart()
    
    def prepareIpynbItems(self):
        Clusterer.prepareIpynbItems(self)
        self.ipynb_items["#k_value#"] = self.k_value
